package net.ecpt.hrm.services;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;

import javolution.util.FastList;
import javolution.util.FastMap;

public class HrResignXlsServices {
    public static final String module = HrResignXlsServices.class.getName();


    /**
     * 导入
     */
    public static Map<String, Object> importXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
	Delegator delegator = dctx.getDelegator();
	List<GenericValue> toBeStore = new ArrayList();
	List<String> messageList = new ArrayList<String>();
	List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");
	LocalDispatcher dispatcher = dctx.getDispatcher();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	
	try {
	    int i = 0;
	   
	    for (Map<String, Object> row : sheetList) {
		i = i + 1;
	
			String id = ExcelPoiUtils.getCellString(row, 0);
			String partyId_dicText = ExcelPoiUtils.getCellString(row, 1);
			String content = ExcelPoiUtils.getCellString(row, 2);
			String estimateResignDate = ExcelPoiUtils.getCellString(row, 3);
			//员工姓名,离职内容,创建时间
			
			
			// 判断
			if (UtilValidate.isEmpty(partyId_dicText)) {
			    messageList.add("第" + (i + 1) + "行,员工姓名不能为空。");
			    continue;
			}

			// 判断
			if (UtilValidate.isEmpty(content)) {
			    messageList.add("第" + (i + 1) + "行,离职内容不能为空。");
			    continue;
			}
			// 判断
			if (UtilValidate.isEmpty(estimateResignDate)) {
			    messageList.add("第" + (i + 1) + "行,创建时间不能为空。");
			    continue;
			}

			
			// 创建
			GenericValue entity = null;
			if (UtilValidate.isNotEmpty(id)) {
			   
			    List<GenericValue> entityList = delegator.findByAnd("HrResign",
					    UtilMisc.toMap("id", id), null, false);
				    entity = EntityUtil.getFirst(entityList);
				if (UtilValidate.isEmpty(entity)) {
						 id = delegator.getNextSeqId("HrResign");
						 entity = delegator.makeValue("HrResign", UtilMisc.toMap("id", id));
				}
					   
			}else  {
			    id = delegator.getNextSeqId("HrResign");
			    entity = delegator.makeValue("HrResign", UtilMisc.toMap("id", id));
			} 
			
			
			
			
			List<GenericValue> personInfo = delegator.findByAnd("Person", UtilMisc.toMap("name", partyId_dicText),null, false);
			GenericValue person =EntityUtil.getFirst(personInfo);
			String partyId = person.getString("partyId");
			
			Timestamp estimateResignDateTimestamp =UtilDateTime.toTimestamp(estimateResignDate);
			
			entity.set("partyId", partyId);
			entity.set("content", content);
			entity.set("estimateResignDate", estimateResignDateTimestamp);
			
			toBeStore.add(entity);
			

			
		}
	    
	    delegator.storeAll(toBeStore);
	    result.put("messageList", messageList);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    /**
     * 导出
     */

    public static Map<String, Object> exportXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Delegator delegator = dctx.getDelegator();
	String ids = (String) context.get("ids");
	List<String> baseIdList = FastList.newInstance();
	if (UtilValidate.isNotEmpty(ids)) {
	    baseIdList = Arrays.asList(ids.split(";"));
	}
	Map<String, Object> result = ServiceUtil.returnSuccess();

	EntityCondition mainCond = null;
	List<EntityCondition> andExprs = FastList.newInstance();
	//andExprs.add(EntityCondition.makeCondition("parentProductId", EntityOperator.EQUALS, "_NA_"));
	if (UtilValidate.isNotEmpty(baseIdList))
	    andExprs.add(EntityCondition.makeCondition("id", EntityOperator.IN, baseIdList));
	if (andExprs.size() > 0)
	    mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);

	try {
	    List<GenericValue> entityList = delegator.findList("HrResign", mainCond, null, null, null, true);

	    List<Map<String, Object>> list = FastList.newInstance();
	    Map<String, Object> keyMap = FastMap.newInstance();
	    keyMap.put("0", "id");
	    keyMap.put("1", "partyId_dicText");
	    keyMap.put("2", "content");
	    keyMap.put("3", "estimateResignDate");
	    
	    String headers = "编号,员工姓名,离职内容,创建时间";
	    result.put("headers", headers);
	    
	    
	    

	    for (GenericValue entity : entityList) {
		Map<String, Object> map = FastMap.newInstance();
		String id = entity.getString("id");
		String partyId = entity.getString("partyId");
		String content = entity.getString("content");
		Timestamp estimateResignDate = entity.getTimestamp("estimateResignDate");
		
		
		GenericValue person = delegator.findOne("Person", UtilMisc.toMap("partyId", partyId), false);
	    String partyId_dicText = person.getString("name");
		
		map.put("id", id);
		map.put("partyId_dicText", partyId_dicText);
		map.put("content", content);
		map.put("estimateResignDate", estimateResignDate);
		
		list.add(map);
	    }
	    result.put("list", list);
	    result.put("keyMap", keyMap);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    
}
